//
//  UIColor+MTHex.m
//  MotorTravel
//
//  Created by zk on 16/9/12.
//  Copyright © 2016年 zk. All rights reserved.
//

#import "UIColor+MTHex.h"

@implementation UIColor (MTHex)

+ (UIColor *)colorForHex:(NSString *)hexColor {
    if (hexColor.length >= 6) {
        NSRange range;
        range.location = 0;
        range.length = 2;
        NSString *rString = [hexColor substringWithRange:range];
        range.location = 2;
        NSString *gString = [hexColor substringWithRange:range];
        range.location = 4;
        NSString *bString = [hexColor substringWithRange:range];
        
        unsigned int r, g, b;
        [[NSScanner scannerWithString:rString] scanHexInt:&r];
        [[NSScanner scannerWithString:gString] scanHexInt:&g];
        [[NSScanner scannerWithString:bString] scanHexInt:&b];
        
        return [UIColor colorWithRed:((float) r / 255.0f)
                               green:((float) g / 255.0f)
                                blue:((float) b / 255.0f)
                               alpha:1.0f];
    } else {
        return [UIColor blackColor];
    }
}

@end
